import scrapy
import jsonpath
from LolScrapy.items import LolscrapyItem


class LolspiderSpider(scrapy.Spider):
    name = 'lolspider'
    allowed_domains = ['game.qtimq.cn']
    start_urls = ['https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js?ts=2749542']

    def parse(self, response):
        json_data = response.json()
        hreo_id = jsonpath.jsonpath(json_data, '$..heroId')[:20]
        for heroid in hreo_id:
            join_url = 'https://game.gtimg.cn/images/lol/act/img/js/hero/{}.js?ts=2749545'.format(heroid)
            yield scrapy.Request(
                url=join_url,
                callback=self.get_queue_url_parse_data,
                dont_filter=True,
            )

    def get_queue_url_parse_data(self,response):

        json_data = response.json()
        skins_list = jsonpath.jsonpath(json_data, '$..skins')[0]
        # logger.info(skins_list)
        heroName = jsonpath.jsonpath(json_data, '$..skins')[0][0]['heroName']


        for skins in skins_list:
            # 皮肤名称
            skins_name = ''.join(skins['name']).replace('/', '')
            "电脑版壁纸"
            win_mianImg = ''.join(skins['mainImg'])
            "手机版壁纸"
            phone_loadingImg = ''.join(skins['loadingImg'])
            if (win_mianImg == '') or (phone_loadingImg == ''):
                continue
            print(skins_name)
            print("电脑壁纸:{}".format(win_mianImg))
            print("手机壁纸:{}".format(phone_loadingImg))

            # # self.process_run(win_mianImg, phone_loadingImg, os_path, skins_name)
            if 'big' in win_mianImg:
                version = 'PC'
                item = LolscrapyItem()
                item['skins_name'] = skins_name
                item['heroName'] = heroName
                item['version'] = version
                list_item = []
                list_item.append(win_mianImg)
                item['win_mainImg'] = list_item
                print(item)
                yield item
                print('{}电脑版保存完毕'.format(skins_name))

            if 'loading' in phone_loadingImg:
                version = 'APP'
                item = LolscrapyItem()
                item['skins_name'] = skins_name
                item['heroName'] = heroName
                item['version'] = version
                list_item = []
                list_item.append(phone_loadingImg)
                item['phone_loadingImg'] = list_item
                print(item)
                yield item
                print('{}手机版保存完毕'.format(skins_name))




